package com.rongshu.view.home.presenter;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.home.contract.DaySignDetailsView;
import com.rongshu.view.home.entity.DaySignDetailsResponse;
import com.rongshu.widget.BanyanDialog;
import com.widget.toast.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

/**
 * created by zhengweis on 2019/2/18.
 * description：
 */
public class DaySignPresenter extends BasePresenter<DaySignDetailsView> {

    public DaySignPresenter(Context context) {
        super(context);
    }

    public void getDaySignInfo(int type, int obj_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("obj_id", obj_id);
        map.put("user_id", UserInfoUtils.getUserId());
        RetorfitService.getDaySignInfo(type, obj_id, UserInfoUtils.getUserId(), Common.encryptionSign(map))
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .subscribe(new Subscriber<DaySignDetailsResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(DaySignDetailsResponse object) {
                        if (object.getErrCode() == 0 && object.getResponse() != null) {
                            getView().success(object.getResponse(), 0);
                        }
                    }
                });
    }

    public void confirmDeleteSign(final String signId) {
        BanyanDialog.Builder builder = new BanyanDialog.Builder(mContext);
        builder.setTitle("提示");
        builder.setContent("确定要删除当前日签吗");
        builder.setLeft("取消");
        builder.setRight("确定");
        final BanyanDialog dialog = builder.create();
        builder.setDialogOnClick(new BanyanDialog.Builder.onClickListener() {
            @Override
            public void leftClickListener() {
                dialog.dismiss();
            }

            @Override
            public void rightClickListener() {
                deleteDaySign(signId);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    /**
     * 删除日签
     */
    public void deleteDaySign(String signId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("signMakingIdStr", signId);
        RetorfitService.deleteDaySign(UserInfoUtils.getUserId(), signId,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(INetBean object) {
                if (object.getErrCode() == 0) {
                    ToastUtils.show("删除制作日签成功");
                    getView().deleteDaySignSuccess();
                } else {
                    ToastUtils.show(object.getErrMsg());
                }
            }
        });
    }
}
